﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AllenData
{
    public class InteractionModeData
    {
        public InteractionMode GetInteractionMode(int interactionModeId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                var interactionMode = ctx.InteractionModes.Where(d => d.InteractionModeID == interactionModeId);
                return interactionMode.FirstOrDefault();
            }
        }

        public List<InteractionMode> GetAllInteractionModes()
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                return ctx.InteractionModes.ToList();
            }
        }

        public int AddNewInteractionMode(InteractionMode interactionMode)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                ctx.AddToInteractionModes(interactionMode);
                ctx.SaveChanges();

                InteractionMode newInteractionMode = ctx.InteractionModes.Where(
                    d => d.Name == interactionMode.Name).FirstOrDefault();

                if (newInteractionMode == null)
                    return -1;
                else
                    return newInteractionMode.InteractionModeID;
            }
        }

        public void UpdateInteractionMode(InteractionMode interactionMode)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                InteractionMode dbDept = ctx.InteractionModes.First(d => d.InteractionModeID == interactionMode.InteractionModeID);
                ctx.ApplyCurrentValues("InteractionModes", interactionMode);
                ctx.SaveChanges();
            }
        }

        public void DeleteInteractionMode(int interactionModeId)
        {
            using (var ctx = new AllenNeighborhoodEntities())
            {
                InteractionMode dbDept = ctx.InteractionModes.First(d => d.InteractionModeID == interactionModeId);
                ctx.DeleteObject(dbDept);
                ctx.SaveChanges();
            }
        }
    }
}
